package com.meyoung.day2;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.util.List;

public class ActionTest2 {

    WebDriver driver;

    /*
     * 我要点击百度首页的新闻链接
     * */
    @BeforeMethod
    public void openChrome() {
        System.setProperty("phantomjs.binary.path", "C:\\Users\\leon1\\IdeaProjects\\web automation\\SeleniumDemo202101\\drivers\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
    }

    /*
    * 打开百度页面
    * 在百度一下按钮上右键单击
    * */
    @Test
    public void rightClick()throws InterruptedException{
        driver.get("http://www.baidu.com");
        WebElement baiduButton = driver.findElement(By.id("su"));
//        实例化Action
        Actions actions = new Actions(driver);
//        百度一下右键
        actions.contextClick(baiduButton).click();
        Thread.sleep(5000);
    }

    /*
     * 打开百度页面
     * 在百度一下按钮上右键单击
     * */
    @Test
    public void doubleClickTest()throws InterruptedException{
        driver.get("http://www.baidu.com");
        WebElement baiduButton = driver.findElement(By.id("su"));
        Thread.sleep(3000);
        Actions actions = new Actions(driver);
        actions.doubleClick().perform();
    }

    /*
    * 元素移动上去
    * 那么显示Hello World!
    * */
    @Test
    public void move()throws InterruptedException{
        driver.get("C:\\Users\\leon1\\Documents\\1.Web自动化selenium（java语言版）\\源码\\webdriver_demo\\selenium_html\\index.html");
        Thread.sleep(3000);
        WebElement element = driver.findElement(By.xpath("//*[@id='action']/input"));
        Actions actions = new Actions(driver);
        actions.moveToElement(element).perform();

        String text = driver.findElement(By.xpath(".//*[text()='Hello World!']")).getText();
        Assert.assertEquals(text,"Hello World!");
    }

    @Test
    public void testDrop()throws InterruptedException{
        driver.get("C:\\Users\\leon1\\Documents\\1.Web自动化selenium（java语言版）\\源码\\webdriver_demo\\selenium_html\\dragAndDrop.html");
        Thread.sleep(3000);
        WebElement element = driver.findElement(By.id("drag"));
        Actions actions = new Actions(driver);
        actions.dragAndDropBy(element,500,500).perform();
    }

    @Test
    public void dropTest()throws InterruptedException{
        driver.get("C:\\Users\\leon1\\Documents\\1.Web自动化selenium（java语言版）\\源码\\webdriver_demo\\selenium_html\\dragAndDrop.html");
        Thread.sleep(5000);
        WebElement element1 = driver.findElement(By.id("drag"));
        WebElement element2 = driver.findElement(By.xpath("/html/body/h1"));
        Thread.sleep(5000);

        Actions actions = new Actions(driver);
        actions.clickAndHold(element1)
                .moveToElement(element2)
                .release(element1)
                .perform();
    }

    @Test
    public void moreSelectTest()throws InterruptedException{
        driver.get("C:\\Users\\leon1\\Documents\\1.Web自动化selenium（java语言版）\\源码\\webdriver_demo\\selenium_html\\index.html");
        Thread.sleep(5000);
        WebElement e1 = driver.findElement(By.id("selectWithMultipleEqualsMultiple"));
        List<WebElement> list = driver.findElements(By.xpath("//*[@id=\"selectWithMultipleEqualsMultiple\"]/option"));
        Thread.sleep(5000);
        Actions actions = new Actions(driver);
        actions.click(list.get(0))
                .keyDown(Keys.CONTROL)
                .click(list.get(2))
                .keyUp(Keys.CONTROL)
                .perform();
        Thread.sleep(5000);
    }

    @Test
    public void saveHtml()throws AWTException,InterruptedException{
        driver.get("http://www.baidu.com");
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_CONTROL);
//        robot.keyPress(KeyEvent.VK_S);
        robot.keyPress(83);
        Thread.sleep(5000);
        robot.keyPress(KeyEvent.VK_ENTER);
    }

    @Test
    public void uploadTest()throws InterruptedException{
        driver.get("C:\\Users\\leon1\\Documents\\1.Web自动化selenium（java语言版）\\源码\\webdriver_demo\\selenium_html\\index.html");
        driver.findElement(By.id("load"))
                .sendKeys("C:\\Users\\leon1\\Desktop\\123.png");
        Thread.sleep(5000);
    }

//    @AfterMethod
//    public void quitTest() {
//        driver.quit();
//    }
}
